package cn.tedu.fresheveryday.service.impl;

import cn.tedu.fresheveryday.exception.ServiceException;
import cn.tedu.fresheveryday.mapper.UserLoginHistoryMapper;
import cn.tedu.fresheveryday.mapper.UserMapper;
import cn.tedu.fresheveryday.pojo.entity.User;
import cn.tedu.fresheveryday.pojo.entity.UserLoginHistory;
import cn.tedu.fresheveryday.pojo.dto.UserLoginDTO;
import cn.tedu.fresheveryday.pojo.dto.UserRegisterDTO;
import cn.tedu.fresheveryday.pojo.vo.UserSimplePhoneVO;
import cn.tedu.fresheveryday.restful.ResponseCode;
import cn.tedu.fresheveryday.service.IUserService;
import cn.tedu.fresheveryday.util.IpUtils;
import cn.tedu.fresheveryday.util.PasswordEncoder;
import cn.tedu.fresheveryday.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    private final UserMapper mapper;
    private final UserLoginHistoryMapper loginHistoryMapper;

    @Autowired
    public UserServiceImpl(UserMapper mapper, UserLoginHistoryMapper loginHistoryMapper) {
        this.mapper = mapper;
        this.loginHistoryMapper = loginHistoryMapper;
    }

    @Override
    public void reg(UserRegisterDTO userRegisterDTO) {
        // 通过用户名查询用户信息
        User dbResult = mapper.selectByUsername(userRegisterDTO.getUsername());
        // 通过手机号查询用户信息
        UserSimplePhoneVO userSimplePhoneVO = mapper.selectByPhone(userRegisterDTO.getPhone());

        if (dbResult != null) {
            throw new ServiceException(ResponseCode.CONFLICT, "用户名已存在");
        }
        if (userSimplePhoneVO != null) {
            throw new ServiceException(ResponseCode.CONFLICT, "手机号已存在");
        }

        User user = new User();

        user.setId(UUID.randomUUID().toString());
        BeanUtils.copyProperties(userRegisterDTO, user);

        // 对密码进行加密，
        String password = user.getPassword();
        String encodedPassword = PasswordEncoder.encode(password);
        // 重新设置用户密码位加密后的密码
        user.setPassword(encodedPassword);

        mapper.insert(user);
    }

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        // 根据用户名查询用户信息
        User user = mapper.selectByUsername(userLoginDTO.getUsername());

        if (user == null) {
            throw new ServiceException(ResponseCode.NOT_FOUND, "登录失败，用户名不存在！");
        }
        if (!PasswordEncoder.matches(userLoginDTO.getPassword(), user.getPassword())) {
            throw new ServiceException(ResponseCode.BAD_REQUEST, "登录失败，密码错误！");
        }

        // 添加用户登录历史
        UserLoginHistory history = new UserLoginHistory();

        history.setId(StringUtils.uuid());
        history.setUserId(user.getId());
        history.setLoginIp(IpUtils.getLocalHost());
        history.setLoginTime(LocalDateTime.now());

        loginHistoryMapper.insert(history);

        return user;
    }

}